﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

namespace Nirvana
{
    /// <summary>
    /// 文本曲线运动
    /// </summary>
    [AddComponentMenu("Nirvana/UI/Effects/Curved Text")]
    [RequireComponent(typeof(Text))]
    public class UICurvedText : BaseMeshEffect
    {
        public enum CurveMode
        {
            TextArea,
            FullRect
        }

        [SerializeField]
        private CurveMode curveMode ;

        [SerializeField]
        private AnimationCurve Curve = new AnimationCurve(new Keyframe[] {
            new Keyframe(0f,0f,0f,2f),
            new Keyframe(1f,0f,-2f,0f)
        });

        [SerializeField]
        private float Strength = 1f;
        [SerializeField]
        private float Frequency = 1f;
        [SerializeField]
        private float Phase;

        public override void ModifyMesh(VertexHelper vh)
        {
            if(!this.IsActive() || vh.currentVertCount <= 0)
            {
                return;
            }

            List<UIVertex> tempList = VertexLists.Get();
            vh.GetUIVertexStream(tempList);
            List<UIVertex> newlist = ModifyTextVertexs(tempList);
            if(newlist!= null)
            {
                vh.Clear();
                vh.AddUIVertexTriangleStream(newlist);
            }
            VertexLists.Set(newlist);
            return;
        }

#if UNITY_EDITOR
        protected override void OnValidate()
        {
            base.OnValidate();
            if(base.graphic != null)
            {
                base.graphic.SetVerticesDirty();
            }
        }
#endif

        private List<UIVertex> ModifyTextVertexs(List<UIVertex> _list)
        {
            Vector2 leftup = Vector2.zero; //左上顶点
            Vector2 rightdown = Vector2.zero;//右下顶点
            if(this.curveMode == CurveMode.FullRect) //全范围
            {
                Rect rect = (transform as RectTransform).rect;
                leftup = new Vector2(rect.xMin, rect.yMax);
                rightdown = new Vector2(rect.xMax, rect.yMin);
            }
            else //只修改文本区域
            {
                leftup = _list[0].position;
                rightdown = _list[_list.Count - 1].position;
                for(int i = 0; i < _list.Count; i++)
                {
                    if(_list[i].position.x < leftup.x)
                    {
                        leftup.x = _list[i].position.x;
                    }
                    if (_list[i].position.y> leftup.y)
                    {
                        leftup.y = _list[i].position.y;
                    }

                    if (_list[i].position.x > rightdown.x)
                    {
                        rightdown.x = _list[i].position.x;
                    }
                    if (_list[i].position.y < rightdown.y)
                    {
                        rightdown.y = _list[i].position.y;
                    }
                }
            }

            float dist = rightdown.x - leftup.x;
            for(int i = 0; i < _list.Count; i++)
            {
                UIVertex vertex = _list[i];
                float percentage = (vertex.position.x - leftup.x) / dist; //每个顶点占的百分比
                percentage = this.Frequency * (this.Phase + percentage);
                vertex.position.y +=  this.Curve.Evaluate(percentage) * this.Strength; //取曲线上的百分比
                _list[i] = vertex;
            }
            return _list;
        }


    }
}

